package com.example.kotlintest

fun main(){
    var list = mutableListOf<String>("one","two","three","four","five")
    println("list集合内容:$list")

//    flow(list)
    var anyResult = list.any { !it.contains("a") }
    println("anyResult = ${anyResult}")

    var count = list.count { it.contains("o") }
    println("count = ${count}")

    list.forEachIndexed { index, value ->
        println("index = ${index}--value = ${value}")
    }
    var maxBy = list.maxBy { it.length }
    println("maxBy = ${maxBy}")

    var minBy = list.minBy { it.length }
    println("minBy = ${minBy}")


    stringIndex(list)


}

/**
 * 字符串下标截取
 * @param list MutableList<String>
 */
private fun stringIndex(list: MutableList<String>) {
    var str0: StringBuffer = StringBuffer();
    println("kotlin 字符串可以通过下标访问")
    list.forEach {
        //kotlin 字符串可以通过下标访问
        str0?.append(it[0])
    }
    println("str[0] = ${str0}")
}

fun flow(list:MutableList<String>){
    list.toString().toCharArray()  // 将字符串转换成字符数组
        .filter { !it.isWhitespace() }  // 过滤空白字符
        .groupBy { it } // 按照集合中每个字符分组
        .also {
//        groupby返回的结果是一个集合map<Char,List<Char>>。key是字符，value是集合
//        {[=[[], o=[o, o, o], n=[n], e=[e, e, e, e], ,=[,, ,, ,, ,], t=[t, t], w=[w], h=[h], r=[r, r], f=[f, f], u=[u], i=[i], v=[v], ]=[]]}
            println("group分组后的结果:$it")
        }
        .map {
            // key是字符char,
            // it.value是集合。list.value.size是集合的长度，也是key出现的个数
            it.key to  it.value.size
        } // 映射，重新生成新的集合
        .let {
            println("map函数重新生成的List集合：$it")
            it.forEach {
                println("元素:${it.first} , 出现次数:${it.second}")
            }
        }
}